package com.jbit.controller;

import com.jbit.dto.DmUser;
import com.jbit.service.DmUserService;
import com.jbit.utils.DmToken;
import com.jbit.utils.Md5Utils;
import com.jbit.utils.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user/api/p")
public class LoginController {
    @Resource
    private DmUserService dmUserService;

    @PostMapping("/login")
    public ResponseEntity login(@RequestBody DmUser dmUser) {
        DmUser loginUser = dmUserService.queryUserByPhone(dmUser.getPhone());
        if (loginUser != null) {
            String key = Md5Utils.generalKey(dmUser.getPassword());
            if (loginUser.getPassword().equals(key)) {
                Object[] objects = new Object[2];
                objects[0] = loginUser;
                objects[1] = DmToken.getToken(loginUser);
                return ResponseEntity.sendSuccess(objects);
            }
        }
        return ResponseEntity.sendError("1006");
    }
}
